package com.baymax.test.scene.demo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.Player
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.newFlexContainer
import com.baymax.test.scene.newScrollContainer
import kotlinx.coroutines.launch
import java.net.URLDecoder
import java.net.URLEncoder
import java.text.SimpleDateFormat
import java.util.Date

class RTSPScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return  p1.newScrollContainer {
            val player = Player(context,)
            val log :TextView = TextView(context).also {  tv->
                setOnClickListener {
                    tv.text = ""
                }
            }

            val simpleDateFormat = SimpleDateFormat("HH:mm:ss.SSS");
            val data = Date()
            fun logFun(string: String){
               launch {
                   data.time = System.currentTimeMillis()
                   log.append("[${simpleDateFormat.format(data)}] ")
                   log.append(string)
                   log.append("\n")
               }
            }
            addView(log)
            player.setLogger(::logFun)


            val editText = EditText(context)
            val testUrl1 = "rtsp://39.135.33.140:554/rtsprrs.gx.chinamobile.com/000000400000001/2100000001"
            val testUrl2 = "rtsp://39.135.33.140:554/rtsprrs.gx.chinamobile.com/000000400000001/2100000002"
            val testUrl3 = "rtsp://39.135.33.140:554/rtsprrs.gx.chinamobile.com/000000400000001/2100000003"
            val url = requireActivity().intent.getStringExtra("url")?.ifBlank { testUrl1 } ?: testUrl1
            editText.setText(URLDecoder.decode(url))
            addView(editText)

            newFlexContainer {
                addDFView(120 ,100, null)
                addDFView(300, 300, player) {
                    logFun("测试播放地址：http://ad.vod01.icntvcdn.com/media/new/2013/icntv2/media/2019/05/14/H142108PhilipsOHCWilliamOutputProdu15sTM.ts")
                    player.start("http://ad.vod01.icntvcdn.com/media/new/2013/icntv2/media/2019/05/14/H142108PhilipsOHCWilliamOutputProdu15sTM.ts")
                }
            }

            addButton("play"){
                logFun("测试播放地址：${editText.text}")
                player.stop()
                player.start(editText.text.toString())
            }
            addButton("clear log"){
                log.text = ""
            }
            addButton("clear log"){
                log.text = ""
            }
            addButton("clear log"){
                log.text = ""
            }
            addButton("clear log"){
                log.text = ""
            }
        }
    }
}
